package com.bdqn.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bdqn.sys.entity.User;
import com.bdqn.sys.dao.UserMapper;
import com.bdqn.sys.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.sys.vo.UserVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author KazuGin
 * @since 2020-04-23
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {



    @Override
    public User findByUserName(String UserName) throws Exception {
        QueryWrapper<User> queryWrapper=new QueryWrapper<User>();
        queryWrapper.eq("loginname",UserName);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public IPage<User> findUserListByPage(IPage<User> page, UserVo uservo) {
        return baseMapper.findUserListByPage(page,uservo);
    }

    @Override
    public Set<Integer> findUserRoleByUserId(Integer id) {
        return  baseMapper.findUserRoleByUserId(id);
    }

    @Override
    public boolean saveUserRole(Integer userId, String roleIds) {
        try {
            //删除角色用户的关系
            baseMapper.deleteUserAndRole(userId);
            //拆分角色ID
            String[] ids = roleIds.split(",");
            for (int i = 0; i <ids.length ; i++) {
                baseMapper.insertUserRole(userId,ids[i]);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    @Override
    public boolean removeById(Serializable id) {
        //删除关系表sys_role_permission表数据
        //根据用户ID删除角色用户关系表的数据
        baseMapper.deleteUserAndRole(id);
        //删除权限表
        return super.removeById(id);
    }
}
